//
//  main.cpp
//  多态
//
//  Created by linjinzhu on 2017/7/13.
//  Copyright © 2017年 baidu. All rights reserved.
//

#include <iostream>

using namespace std;

class Shape {
protected:
    int width, height;
public:
    Shape(int a,int b) {
        width = a;
        height = b;
    }

    // 非虚函数
    // 没有virtual修饰，编译器会执行静态链接，在函数执行前就已经准备好了
    /**int area() {
        cout << "Parent class area" << endl;
        return 0;
    }*/

    // 虚函数
    // 有virtual修饰，编译器会执行动态链接，程序中任意点可以根据所调用的对象类型来选择调用的函数
    /**virtual int area() {
        cout << "Parent class area" << endl;
        return 0;
    }*/

    // 纯虚函数
    // = 0 告诉编译器，函数没有主体，上面的虚函数是纯虚函数
    virtual int area() = 0;
};

class Rectangle: public Shape {
public:
    Rectangle(int a, int b): Shape(a,b) {}
    int area() {
        cout << "Rectangle class area" << endl;
        return (width * height);
    }
};

class Triangle: public Shape {
public:
    Triangle(int a,int b): Shape(a,b) {}
    int area() {
        cout << "Triangle class area" << endl;
        return (width * height) / double(2);
    }
};

int main(int argc, const char * argv[]) {

    Shape *shape;

    Rectangle rec(2,2);
    Triangle  tri(2,2);

    shape = &rec;
    cout << shape->area() << endl;

    shape = &tri;
    cout << shape->area() << endl;

    return 0;
}
